//
//  NRWCarWorkDeskController.m
//  New_Runway
//
//  Created by mack on 2018/1/24.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarWorkDeskController.h"
#import "DeskMeunCell.h"
#import "DeskMeunHeaderView.h"
#import "WorkDeskMainModel.h"
#import "NoDealNumModel.h"
#import "CarManagerNumModel.h"

@interface NRWCarWorkDeskController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *controllers;
@property (nonatomic, strong) UICollectionView *collectionview;
@property (nonatomic, strong) NSMutableArray *sectionArray;
@property (nonatomic, strong) NSMutableArray *rowArray;
@property (nonatomic, strong) NoDealNumModel *numModel;
@property (nonatomic, strong) CarManagerNumModel *carnumModel;
@property (nonatomic, copy) NSString *roleId;
@property (nonatomic, strong) NSMutableDictionary *params;
@end

@implementation NRWCarWorkDeskController
-(NSMutableArray *)sectionArray{
    if (!_sectionArray) {
        _sectionArray = [[NSMutableArray alloc] init];
    }

    return _sectionArray;
}
-(NSMutableArray *)rowArray{
    
    if (!_rowArray) {
        _rowArray = [[NSMutableArray alloc] init];
    }
    return _rowArray;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.view addSubview:self.collectionview];
    [self loadNumber];
    [self loadCarManagerNum];

}
-(void)loadCarManagerNum{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,carManagerNumApi];
//    NSDictionary *params = @{@"marketId":[NRWCarUser sharedUser].marketId,
//                             @"shopId":[NRWCarUser sharedUser].shopId ? [NRWCarUser sharedUser].shopId : @""
//                             };
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:_params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        
        if ([response[@"code"] isEqual:@1]) {
            self.carnumModel = [[CarManagerNumModel alloc] init];
            [self.carnumModel setValuesForKeysWithDictionary:response[@"result"]];
            [self.collectionview reloadData];
        }
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
    }];
    
    
}
-(void)loadNumber{
    NSString *url = [NSString stringWithFormat:@"%@%@",CRMContextApi,noDealNumApi];
    NSDictionary *params = @{@"followerId":[NRWCarUser sharedUser].userId,
                             @"shopId":[NRWCarUser sharedUser].shopId ? [NRWCarUser sharedUser].shopId : @""
                             };
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {

        if ([response[@"code"] isEqual:@1]) {
            self.numModel = [[NoDealNumModel alloc] init];
            [self.numModel setValuesForKeysWithDictionary:response[@"result"]];
            [self.collectionview reloadData];
        }

    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationBarView.titleLabel.text = @"工作台";
    self.navigationBarView.leftItem.hidden = YES;
    for (NSDictionary *dic in _loginModel.resourceList) {
        WorkDeskMainModel *sectionModel = [WorkDeskMainModel mj_objectWithKeyValues:dic];
        [self.sectionArray addObject:sectionModel];
        
    }
    if ([NRWCarUser sharedUser].roleList.count != 0) {
        NSDictionary *roleDic = [NRWCarUser sharedUser].roleList[0];
        _roleId = roleDic[@"roleId"];
    }
    if ([_roleId integerValue] == 30) {//市场管理员
        _params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NRWCarUser sharedUser].marketId,@"marketId",nil];

    }else if ([_roleId integerValue] == 31){//市场员工
        _params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NRWCarUser sharedUser].marketId,@"marketId",[NRWCarUser sharedUser].userId,@"userId",nil];

    }else if ([_roleId integerValue] == 40 || [_roleId integerValue] == 41){//车商和车商员工
            _params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NRWCarUser sharedUser].marketId,@"marketId",[NRWCarUser sharedUser].shopId,@"shopId",nil];
    }else{
        _params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NRWCarUser sharedUser].marketId,@"marketId",[NRWCarUser sharedUser].shopId ? [NRWCarUser sharedUser].shopId : @"",@"shopId",nil];
    }
    [self.view addSubview:self.collectionview];
   
}

-(UICollectionView *)collectionview{
    if (!_collectionview) {
        
    //创建一个layout布局类
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    //设置布局方向为垂直流布局
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置每个item的大小为100*100
//    layout.itemSize = CGSizeMake(80, 110);
    layout.minimumLineSpacing      = 0;
    layout.minimumInteritemSpacing = 0;
    //    [layout setHeaderReferenceSize:CGSizeMake(Screen_width, self.view.frame.size.width * 3 / 5)];
    //创建collectionView 通过一个布局策略layout来创建
    _collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.navH, Screen_width, Screen_height - self.navH - self.tabH) collectionViewLayout:layout];
    //代理设置
    _collectionview.backgroundColor = kColorWhite();
    _collectionview.delegate=self;
    _collectionview.dataSource=self;
    //注册item类型 这里使用系统的类型
    [_collectionview registerNib:[UINib nibWithNibName:@"DeskMeunCell" bundle:nil] forCellWithReuseIdentifier:@"DeskMeunCell"];
    [_collectionview registerNib:[UINib nibWithNibName:@"DeskMeunHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"DeskMeunHeaderView"];
   }
    return _collectionview;
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.sectionArray.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
     WorkDeskMainModel *sectionModel = self.sectionArray[section];
    
    return sectionModel.resourceList.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    WorkDeskMainModel *sectiomModel = self.sectionArray[indexPath.section];
    WorkDeskMainModel *rowModel = [WorkDeskMainModel mj_objectWithKeyValues:sectiomModel.resourceList[indexPath.row]];
    DeskMeunCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"DeskMeunCell" forIndexPath:indexPath];
    cell.iconIamge.image = [UIImage imageNamed:rowModel.resourceIcon];
    cell.title.text = rowModel.resourceName;
    cell.resourceRemark = rowModel.resourceRemark;
    cell.carNumModel = self.carnumModel;
    cell.model = self.numModel;

    return cell;
}


-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section

{
    return UIEdgeInsetsMake(10, 10, 0, 10);//分别为上、左、下、右
    
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
       NRWCarLoginModel *sectionModel = self.sectionArray[indexPath.section];
        DeskMeunHeaderView *headerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"DeskMeunHeaderView" forIndexPath:indexPath];
    headerview.iconImage.image = [UIImage imageNamed:sectionModel.resourceIcon];
    headerview.title.text = sectionModel.resourceName;

    return headerview;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake((Screen_width - 50) / 4, (Screen_width - 50) / 4  + 40);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(Screen_width, 40);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    WorkDeskMainModel *sectiomModel = self.sectionArray[indexPath.section];
    WorkDeskMainModel *rowModel = [WorkDeskMainModel mj_objectWithKeyValues:sectiomModel.resourceList[indexPath.row]];
    [[CTMediator sharedInstance] Router_viewControllerForDesk:@{@"vc":rowModel.resourceRemark} callback:^(NSDictionary *info) {

    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
}

@end
